﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Web;
using MVC.Models.LINQ;
using MVC.Models.Admin;


namespace MVC.Models.Admin
{
    public class IAccount
    {
        private static LINQDBDataContext data=new LINQDBDataContext();
        private static LAccount laccount = new LAccount();

        public IList<NHANVIEN> Filter(IList<NHANVIEN> acc,IAP ia)//loc danh sach
        {
            IList<NHANVIEN> accounts = acc.ToList();//lay danh sach tai khoan
            if (ia.Activate != IAccessPermission.NONE)//neu quyen truy cap khac none
            {
                accounts = accounts.Where(a => a.KichHoat == ia.Activate).ToList();//gan cho tai khoan bang active
            }
            return accounts;
        }

        public IList<NHANVIEN> getAllAccount()//lay danh sach tat ca tai khoan
        {
            return Filter(data.NHANVIENs.ToList(),
                new IAP {
                Access=IAccessPermission.NONE,
                Activate=IAccessPermission.NONE
                });
        }
        //lay danh sach tai khoan theo trang
        public LAccount getAllAccount(int page,int pagesize,IAP ia)
        {
            laccount = new LAccount();
            laccount.Accounts = data.NHANVIENs.ToList();//lay danh sach tu linq to sql
            laccount.total = laccount.Accounts.Count;//tong so account
            laccount.Accounts = laccount.Accounts.Skip(page).Take(pagesize).ToList();//so tai khoan tren trang
            laccount.Accounts = Filter(laccount.Accounts, ia);
            laccount.page = page;
            laccount.pagesize = pagesize;
            return laccount;
        }
        //lay danh sach tai khoan theo trang thai
        private LAccount getAllAccount(int page,int status, int pagesize, IAP ia)
        {
            laccount = new LAccount();
            laccount.Accounts = laccount.Accounts.Where(a => a.TrangThai == status).ToList();
            laccount.Accounts = data.NHANVIENs.ToList();//lay danh sach tu linq to sql
            laccount.total = laccount.Accounts.Count;//tong so account
            laccount.Accounts = laccount.Accounts.Skip(page).Take(pagesize).ToList();//so tai khoan tren trang
            laccount.Accounts = Filter(laccount.Accounts, ia);
            laccount.page = page;
            laccount.pagesize = pagesize;
            return laccount;
        }
        //lay tai khoan theo id
        public NHANVIEN getAccount(int id)
        {
            NHANVIEN acc = data.NHANVIENs.Single(a => a.MaNV == id);
            return acc;
        }
        //lay tai khoan theo username
        public NHANVIEN getAccount(string username)
        {
            NHANVIEN acc = data.NHANVIENs.Single(a => a.TenTaiKhoan.Trim() == username.Trim());
            return acc;
        }
        //them tai khoan
        public void AddAccount(NHANVIEN _account)
        {
            data.NHANVIENs.InsertOnSubmit(_account);
            data.SubmitChanges();
        }
        //cap nhat tai khoan
        public void UpdateAccount(NHANVIEN _account)
        {
            try
            {
                NHANVIEN acc = data.NHANVIENs.Single(a => a.MaNV == _account.MaNV);//cap nhat theo id tai khoan
                acc.MaCV = _account.MaCV;
                acc.TenNV = _account.TenNV;
                acc.MaBP = _account.MaBP;
                acc.TenTaiKhoan = _account.TenTaiKhoan;
                
                acc.Email = _account.Email;
                acc.KichHoat = _account.KichHoat;
                acc.ThuTu = _account.ThuTu;
                acc.QuyenTruyCap = _account.QuyenTruyCap;
                
                if(_account.MatKhau.Length>0)
                    acc.MatKhau = _account.MatKhau;
                data.SubmitChanges();
            }
            catch(Exception ex)
            {
                
            }
        }
        //xoa tai khoan theo id
        public void RemoveAccount(int id)
        {
            try
            {
                NHANVIEN acc = data.NHANVIENs.Single(a=>a.MaNV==id);
                data.NHANVIENs.DeleteOnSubmit(acc);
                data.SubmitChanges();
            }
            catch(Exception ex)
            {
            }
        }
        //kiem tra dang nhap
        public NHANVIEN checkLogin(string username, string password)
        {
            try
            {
                //kiem tra tai khoan da dung usename, password va da active chua
                NHANVIEN acc = data.NHANVIENs.Single(a => a.TenTaiKhoan == username && a.MatKhau == password && a.KichHoat == IAccessPermission.ACTIVE);
                return acc;
            }
            catch (Exception ex)
            {
            }
            return null;
        }
        //kiem tra ton tai username va email
        public bool isExist(string username,string email)
        {
            try
            {
                NHANVIEN acc = data.NHANVIENs.Single(a => a.TenTaiKhoan == username || a.Email == email);
                return true;
            }
            catch
            {
            }
            return false;
        }
        //set trang thai nguoi dung
        public void setStatus(int id, int status, DateTime date)
        {
            try
            {
                NHANVIEN acc = data.NHANVIENs.Single(a => a.MaNV == id);
                acc.TrangThai = status;
                if (date != null)
                    acc.ThoiGianTruyCap = date;
                data.SubmitChanges();
            }
            catch
            {
            }
        }
        //cap nhat trang thai cua tai khoan
        public void UpdateScanStatus()
        {
            //tim tai khoan voi dieu kien <10 phut
            var _account = (from a in data.NHANVIENs
                       where SqlMethods.DateDiffMinute(a.ThoiGianTruyCap, DateTime.Now) < 10
                       select a);
            //lap nhung tai khoan trong thoi gian tren
            foreach (NHANVIEN acc in _account)
            {
                acc.TrangThai = 0;
            }
            data.SubmitChanges();

        }

    }
    public class LAccount//ListAccount
    {
        public IList<NHANVIEN> Accounts { get; set;}
        public int total { get; set; }
        public int pagesize { get; set; }
        public int page { get; set; }
    }
}